package yx.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.CodeServiceInt;
import yx.vo.CodeVO;
import yx.vo.DatagridVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "codeAction")
public class CodeAction extends BaseAction implements ModelDriven<CodeVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CodeAction.class);

	private CodeVO code = new CodeVO();

	@Override
	public CodeVO getModel() {
		return code;
	}

	private CodeServiceInt codeService;

	public CodeServiceInt getCodeService() {
		return codeService;
	}

	@Autowired
	public void setCodeService(CodeServiceInt codeService) {
		this.codeService = codeService;
	}

	public void queryCode() {
		super.writeJson(codeService.datagrid(code));
	}
	
	public void queryCodeList() {
		DatagridVO datagridVO = codeService.datagrid(code);
		if(datagridVO.getTotal().intValue() != 0){
			List list = datagridVO.getRows();
			super.writeJson(list);
		}
	}

	public void addCode() {
		JsonVO j = new JsonVO();
		try {
			CodeVO u = codeService.save(code);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeCode() {
		codeService.remove(code.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editCode() {
		CodeVO u = codeService.edit(code);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
